/*
 * create:2016年4月16日 下午12:42:03
 * author:wangds  
 */
package net.wangds.comp.endpoint.http.interpreter;

import java.io.File;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileCleaningTracker;

import net.wangds.comp.endpoint.http.AbstractHttpRequestInterpreter;
import net.wangds.comp.endpoint.http.FormBasedFileUploadRequestForm;

/**
 * .
 * 
 * @author wangds
 *
 */
public class ReqFormBasedUploadInterpreter extends AbstractHttpRequestInterpreter<FormBasedFileUploadRequestForm> {

  private String tmpRepositoryPath;

  private String encoding = "UTF8";

  /**
   * The setter method of the property encoding.
   * 
   * @param theencoding
   *          the encoding to set
   * @author wangds 2016年5月29日 下午6:56:34.
   */
  public void setEncoding(String encoding) {
    this.encoding = encoding;
  }

  /**
   * The getter method of the property encoding.
   * 
   * @author wangds 2016年5月29日 下午6:56:39.
   * @return the encoding.
   */
  public String getEncoding() {
    return encoding;
  }

  public String getTmpRepositoryPath() {
    return tmpRepositoryPath;
  }

  public void setTmpRepositoryPath(String tmpRepositoryPath) {
    this.tmpRepositoryPath = tmpRepositoryPath;
  }

  public static DiskFileItemFactory newDiskFileItemFactory(ServletContext context, File repository) {
    FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(context);
    DiskFileItemFactory factory = new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository);
    factory.setFileCleaningTracker(fileCleaningTracker);
    return factory;
  }

  /*
   * @see
   * net.wangds.comp.endpoint.http.AbstractHttpRequestInterpreter#interpreter(
   * javax.servlet.http.HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)
   * 
   * @author wangds 2016年4月16日 下午12:43:05.
   */
  @Override
  protected FormBasedFileUploadRequestForm interpreter(HttpServletRequest req, HttpServletResponse resp) {
    FormBasedFileUploadRequestForm form = new FormBasedFileUploadRequestForm();
    try {
      boolean isMultipart = ServletFileUpload.isMultipartContent(req);

      Map<String, String> form_field_map = new HashMap<String, String>();
      List<FileItem> file_items_list = new LinkedList<FileItem>();
      
      if (isMultipart) {
        DiskFileItemFactory factory = newDiskFileItemFactory(req.getSession().getServletContext(), new File(this.getTmpRepositoryPath()));
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding(encoding);
        List<FileItem> items = upload.parseRequest(req);
        for (FileItem item : items) {
          if (item.isFormField()) {
            String name = item.getFieldName();
            String value = item.getString();
            form_field_map.put(name, value);
          } else {
            file_items_list.add(item);
          }
        }

      }else{
        Enumeration<String> names = req.getParameterNames();
        while(names.hasMoreElements()){
          String name = names.nextElement();
          String value = req.getParameter(name);
          form_field_map.put(name, value);
        }
      }
      
      form.setFields(form_field_map);
      form.setFileItems(file_items_list);
      
    } catch (Exception e) {
      this.getLogger().error(e.getLocalizedMessage(),e);
    }
    return form;
  }

}
